package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategorySevice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    private CategorySevice  categorySevice;

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation("新增分类")
    @PostMapping
    public Result<String> addCategory(@RequestBody CategoryDTO  categoryDTO){
        log.info("新增分类：{}",categoryDTO);
        boolean flag = categorySevice.save(categoryDTO);
        if(!flag){
            return Result.error("分类名称或排序重复");
        }else {
            return Result.success();
        }
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> PageQuery(CategoryPageQueryDTO  categoryPageQueryDTO){
        log.info("分页查询：{}",categoryPageQueryDTO);
        PageResult pageResult = categorySevice.pagequery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @ApiOperation("删除分类")
    @DeleteMapping
    public Result<String> Delete(Integer id){
        boolean flag = categorySevice.delete(id);
        if(!flag){
            return Result.error("与菜品关联，删除失败！");
        }else{
            return Result.success();
        }
    }

    /**
     * 修改分类状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("修改分类状态")
    @PostMapping("/status/{status}")
    public Result<String> changeStatus(@PathVariable Integer status,long id){
        log.info("修改分类状态：{}{}",id,status);
        categorySevice.changeStatus(status,id);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result<String> editCate(@RequestBody CategoryDTO  categoryDTO){
        boolean flag = categorySevice.editCategory(categoryDTO);
        if(!flag){
            return Result.error("分类名称或排序重复");
        }else {
            return Result.success();
        }
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    //在菜品管理时用到
    public Result<List<Category>> getCategoryByType(Integer type){
        List<Category> categories = categorySevice.getCategoryByType(type);
        return Result.success(categories);
    }
}
